package com.study.javaBasic.reflection;

/**
 * 反射:
 * @author WS
 */
public class ClassInfo {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        printClassInfo("".getClass());
//        printClassInfo(Runnable.class);
//        printClassInfo(java.time.Month.class);
//        printClassInfo(String[].class);
//        printClassInfo(int.class);
//        getClassMethod();
//        getInstance();
    }

    static void printClassInfo(Class cls) {
        System.out.println("类名: " + cls.getName());
        System.out.println("    简名: " + cls.getSimpleName());
        if (cls.getPackage() != null) {
            System.out.println("    包名: " + cls.getPackage().getName());
        }
        System.out.println("    is 接口: " + cls.isInterface());
        System.out.println("    is 枚举: " + cls.isEnum());
        System.out.println("    is 数组: " + cls.isArray());
        System.out.println("    is 原函数: " + cls.isPrimitive());
        System.out.println("    父类:" + cls.getSuperclass());
        System.out.println("    接口:" + cls.getInterfaces());
        System.out.println("    是否可以转换为Object:" + Object.class.isAssignableFrom(cls));
        System.out.println("----------------------");
    }

    static void getClassMethod() throws ClassNotFoundException {
        Class cls1 = String.class;
        Class cls2 = "String".getClass();
        Class cls3 = Class.forName("java.lang.String");
        Class cls4 = ClassLoader.getSystemClassLoader().loadClass("java.lang.String");
        printClassInfo(cls1);
        printClassInfo(cls2);
        printClassInfo(cls3);
        printClassInfo(cls4);
    }

    static void getInstance() throws IllegalAccessException, InstantiationException {
        Class cls1 = String.class;
        String o = (String) cls1.newInstance();
        System.out.println("String实例：" + o);
    }
}
